/***
 * @Author: LVGRAPE
 * @Date: 2023-09-22 11:04:09
 * @LastEditTime: 2023-09-22 11:21:04
 * @LastEditors: LVGRAPE
 * @Description:
 * @FilePath: \ZINO_FC_V4\ZINO\hardware\si24r1\si24r1.h
 * @要啥没啥，爱咋咋�??
 */

#ifndef __SI24R1_H_
#define __SI24R1_H_
#ifdef __cplusplus
extern "C"
{
#endif // __cplusplus

#include "usr_def.h"

    enum si24r1_reg_map {
        SI24R1_REG_CONFIG = 0,//配置寄存�??
        SI24R1_REG_ENAA,//使能自动确认
        SI24R1_REG_EN_RXADDR,//使能接收数据管道地址
        SI24R1_REG_SETUP_AW,//地址宽度配置
        SI24R1_REG_SETUP_RETR,//自动重发配置
        SI24R1_REG_RF_CH,//射频信道
        SI24R1_REG_RF_SETUP,//射频配置
        SI24R1_REG_STATUS,//状态寄存器（SPI操作开始，状态寄存器值通过MISO串行输出)�??
        SI24R1_REG_OBSERVE_TX,//发射结果统计
        SI24R1_REG_RSSI,//接收信号强度检�??
        SI24R1_REG_RX_ADDR_P0,//数据管道0的接收地址，最大宽度为5bytes(LSByte最先写入，通过SETUP_AW配置地址宽度) �??
        SI24R1_REG_RX_ADDR_P1,//数据管道1的接收地址，最大宽度为5bytes(LSByte最先写入，通过SETUP_AW配置地址宽度) �??
        SI24R1_REG_RX_ADDR_P2,//数据管道2的接收地址，最大宽度为5bytes(LSByte最先写入，通过SETUP_AW配置地址宽度) �??
        SI24R1_REG_RX_ADDR_P3,//数据管道3的接收地址，最大宽度为5bytes(LSByte最先写入，通过SETUP_AW配置地址宽度) �??
        SI24R1_REG_RX_ADDR_P4,//数据管道4的接收地址，最大宽度为5bytes(LSByte最先写入，通过SETUP_AW配置地址宽度) �??
        SI24R1_REG_RX_ADDR_P5,//数据管道5的接收地址，最大宽度为5bytes(LSByte最先写入，通过SETUP_AW配置地址宽度) �??
        SI24R1_REG_TX_ADDR,//发射方的发射地址(LSByte最先写�??)，如果发射放需要收ACK确认信号，则需要配置RX_ADDR_P0的值等于TX_ADDR，并使能ARQ�??
        SI24R1_REG_RX_PW_P0,//接收数据管道0数据字节�??
        SI24R1_REG_RX_PW_P1,//接收数据管道1数据字节�??
        SI24R1_REG_RX_PW_P2,//接收数据管道2数据字节�??
        SI24R1_REG_RX_PW_P3,//接收数据管道3数据字节�??
        SI24R1_REG_RX_PW_P4,//接收数据管道4数据字节�??
        SI24R1_REG_RX_PW_P5,//接收数据管道5数据字节�??
        SI24R1_REG_FIFO_STATUS,//FIFO状�?
        SI24R1_REG_DYNPD = 0x1C,//使能动态负载长�??
        SI24R1_REG_FEATURE,//特征寄存�??
    };
    enum si24r1_commands {
        SI24R1_CMD_R_REGISTER = 0X1F,//000A AAAA,1 to 5 LSByte first,读寄存器命令�?? AAAAA表示寄存器地址（参考寄存器表）�??
        SI24R1_CMD_W_REGISTER = 0X20,//001A AAAA,1 to 5 LSByte first,读寄存器命令�?? AAAAA表示寄存器地址（参考寄存器表）。只允许Shutdown�?? Standby�?? Idle-TX模式下操作�?
        SI24R1_CMD_R_RX_PAYLOAD = 0X61,//0110 0001 从FIFO中读收到的数据， 1-32字节，读出后FIFO数据被删除。适用于接收模式�?
        SI24R1_CMD_W_TX_PAYLOAD = 0XA0,//1010 0000 写发射负载数据，大小�??1-32字节，适用于发射模式�?
        SI24R1_CMD_FLUSH_TX = 0XE1,//1110 0001 清空TX FIFO，适用于发射模式�?
        SI24R1_CMD_FLUSH_RX = 0XE2,//1110 0010 清空RX FIFO，适用于接收模式。如果需要回ACK，则不能在回ACK操作完成前进行清空FIFO，否则视为通信失败�??
        SI24R1_CMD_REUSE_TX_PL = 0XE3,//1110 0011 适用于发送方，清空TX FIFO或对FIFO写入新的数据后不能使用该命令�??
        SI24R1_CMD_R_RX_PL_WID = 0X60,//0110 0000 读取收到的数据字节数�??
        SI24R1_CMD_W_ACK_PAYLOAD = 0XA8,//1010 1PPP 适用于接收方，通过PIPE PPP将数据通过ACK的形式发出去，最多允许三帧数据存于FIFO中�?
        SI24R1_CMD_W_TX_PAYLOAD_NOACK = 0XB0,//1011 0000 适用于发射模式，使用这个命令同时需要将AUTOACK位置1�??
        SI24R1_CMD_NOP = 0XFF,//1111 1111 无操作。可用于返回STATUS值�?
    };
    typedef enum //NRF CONFIG
    {
        EN_CRC = 0X08,
        CRCO_2BYTE=0X04,
        PWR_UP=0X02,
        PRIM_RX=0X01,
    } si24r1_config_e;
    typedef enum //NRF_STATUS
    {
        CLEAR_RX_IRQ = 1 << 6,
        CLEAR_TX_IRQ = 1 << 5,
        CLEAR_MAX_RT_IRQ = 1 << 4,
        CLEAR_NOT_USED = 6 << 1,
        CLEAR_RX_FIFI_EMPTY = 7 << 1,
        CLEAR_TX_FULL = 1 << 0,
    } SI24R1_STATUS_E;
    enum SI24R1_ROLE {
        SI24R1_ROLE_TX = 0,
        SI24R1_ROLE_RX = 1,
    };
    enum SI24R1_TX_POWER {
        SI24R1_TX_POWER_N12dBm = 0,
        SI24R1_TX_POWER_N6dBm,
        SI24R1_TX_POWER_N4dBm,
        SI24R1_TX_POWER_0dBm,
        SI24R1_TX_POWER_1dBm,
        SI24R1_TX_POWER_3dBm,
        SI24R1_TX_POWER_4dBm,
        SI24R1_TX_POWER_7dBm,
    };
    enum SI24R1_BANDWITH {
        SI24R1_BANDWITH_1M = 0,
        SI24R1_BANDWITH_2M,
        SI24R1_BANDWITH_250K,
    };
    typedef struct si24r1_user_config {
        enum SI24R1_ROLE role;
        enum SI24R1_TX_POWER tx_power;
        enum SI24R1_BANDWITH band;
        uint8_t channel;
        uint8_t address[5];
    }si24r1_user_t;
    enum si24r1_rf_setup {
        SI24R1_RF_SETUP_1Mbps = 0X00,
        SI24R1_RF_SETUP_2Mbps = 0X08,
        SI24R1_RF_SETUP_250kbps = 0X20,
        SI24R1_RF_SETUP_7dBm = 7,
        SI24R1_RF_SETUP_4dBm = 6,
        SI24R1_RF_SETUP_3dBm = 5,
        SI24R1_RF_SETUP_1dBm = 4,
        SI24R1_RF_SETUP_0dBm = 3,
        SI24R1_RF_SETUP_N4dBm = 2,
        SI24R1_RF_SETUP_N6dBm = 1,
        SI24R1_RF_SETUP_N12dBm = 0,
    };
    enum si24r1_feature{
        SI24R1_FEATURE_EN_DPL=1<<2,//使能动态负载长�?
        SI24R1_FEATURE_EN_ACK_PAYd=1<<1,//使能ACK负载(带负载数据的ACK�?)
        SI24R1_FEATURE_DYN_ACK=1,//使能命令W_TX_PAYLOAD_NOACK
    };
    /*	24L01	*/

#define IRQ P21
#define MISO P24
#define MOSI P23
#define SCK P25
#define CSN P26
#define CE  P27

    uint8_t NRF_test();
    void FIFO_write(uint8_t DATA_OUT[], uint8_t lengh);
    void FIFO_read(uint8_t DATA_IN[], uint8_t lengh);
    void TX_address(uint8_t DATA_IN[]);
    void RX_address(uint8_t DATA_IN[]);
    void RX_mode();
    void TX_mode();
    void CLR_RXIT();
    void CLR_TXIT();
    void  NRF_power(uint8_t baudrate, uint8_t tx_power);
    void NRF_size(uint8_t l);
    void REG_write(uint8_t address, uint8_t command);
    uint8_t REG_read(uint8_t address);
    void reg_read_buf(uint8_t address, uint8_t buf[], uint8_t lengh);
    void reg_write_buf(uint8_t address, uint8_t buf[], uint8_t lengh);
    uint8_t read_rx_payload_width();
    void FIFO_clear(void);
    void FIFO_flush(void);
    void fifo_write_ack_payload(uint8_t DATA_OUT[], uint8_t lengh);
    void si24r1_shutdown();
#ifdef __cplusplus
}
#endif // __cplusplus
#endif
